const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    speed= 200;

    a= false;
    d= false;
    w= false;
    s= false;

    onKeyDown (event) {
        if (event.keyCode==cc.macro.KEY.a||event.keyCode==cc.macro.KEY.left) {
            this.a = true;
        }
        if (event.keyCode==cc.macro.KEY.d||event.keyCode==cc.macro.KEY.right) {
            this.d = true;
        }
        if (event.keyCode==cc.macro.KEY.w||event.keyCode==cc.macro.KEY.right) {
            this.w = true;
        }
        if (event.keyCode==cc.macro.KEY.s||event.keyCode==cc.macro.KEY.right) {
            this.s = true;
        }     
    }

    onkeyup(event) {
        if (event.keyCode==cc.macro.KEY.a||event.keyCode==cc.macro.KEY.left) {
            this.a = false;
        }
        if (event.keyCode==cc.macro.KEY.d||event.keyCode==cc.macro.KEY.right) {
            this.d = false;
        }
        if (event.keyCode==cc.macro.KEY.w||event.keyCode==cc.macro.KEY.right) {
            this.w = false;
        }
        if (event.keyCode==cc.macro.KEY.s||event.keyCode==cc.macro.KEY.right) {
            this.s = false;
        }     
    }
    
    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onkeyup, this);
    }

    start () {
       
    }

    update (dt) {
        let v2:cc.Vec2;
        if (this.a) {
            v2.x = this.node.x - this.speed * dt;           
        }
        if (this.d) {
            v2.x = this.node.x + this.speed * dt;
        }
        if (this.w) {
            v2.y = this.node.y + this.speed * dt;
        }
        if (this.s) {
            v2.y = this.node.y - this.speed * dt;
        }
        this.node.position = v2;
    }
}
